/**
 *
 * @file sniffer.h
 *
 * @brief Header file for built in sniffer
 * 
 * $Id: sniffer.h 123 2009-01-15 08:42:49Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef SNIFFER_H
#define SNIFFER_H


/* -- S T A N D A R D  I N C L U D E S -- */
#include <unistd.h>


/* -- O T H E R  I N C L U D E S -- */
#include <output.h>
#include <tsar.h>
#include <loop.h>
#include <tsar.h>

/* -- L I B  P C A P  I N C L U D E -- */
#include <pcap.h>


/* -- C O N S T A N T S -- */
/** @def number of milliseconds for packet capture timeout */
#define CAPTURE_TIMEOUT_MS      1000
/** @def number of bytes to capture per packet */
#define CAPTURE_SNAPLEN         65535


/* -- T Y P E D E F S -- */



/* -- F U N C T I O N S  P R O T O T Y P E S -- */
/**
 * @brief startup packets capture service
 * @param o a pointer to an opt_t that contains user or default values to parameters required to start tsar
 * @return a handle to a sniffing session or NULL on error
 * @retval not NULL : success case, everything was OK during this session
 * @retval NULL : error case, an error occured (and a message has been printed)
 */
sniffer_t * start_sniffer( opt_t * o );

/**
 * @brief open a sniffing session
 * @param o structure where are stored user options
 * @param session pointers that constitute the session
 * @return error code according to the execution of the function
 * @retval 0 no error occured
 * @retval 1 pcap failed to open the network device
 * @retval 2 network interface is not an Ethernet device
 * @retval 3 cannot open a pcap_dumper to store caputred traffic
 */
int  open_sniff_session( opt_t * o, sniffer_t * session );

/**
 * @brief apply a filter on the sniffer
 * @param pcap pcap handle onto which apply the filter
 * @param o options structure that contains the filter
 * @return error code accordingto the execution of the function
 * @retval 0 no error
 * @retval 1 invalid filtering expression
 * @retval 2 cannot apply compiled filter
 */
int apply_filter( pcap_t * pcap, opt_t * o );

/**
 * @brief loop that saves network traffic catched to a savefile
 * @param session is a previously opened capture session
 * @return error code according to the execution of the function
 * @retval 0 loop broken by the signal handler
 * @retval 1 loop broken after an error occured
 */
int dump_traffic( sniffer_t * session );

/**
 * @brief shutdown opened services to close session
 * @param session points to the session structure to close
 */
void shutdown_sniffer( sniffer_t * session );

/**
 * @brief callback for detected activity on our pcap descriptor
 * @sock is the pcap socket returned by pcap_get_selectable_fd()
 * @param datas is a void (casted) pointer to the current tsar_t session
 */
void sniffer_dump( int sock, void * datas );

#endif /* SNIFFER_H */

